Page({
  data: {
    listData: [],
    listData2: [],
    infos: [],
    page: 1,
    rows: 10,
    margin: 23,
    leftHeight: 0,
    rightHeight: 0,
    leftPosition: 0,
    rightPosition: 363,
    requestLength: 0,
    count: 0,
    loadedAll: false,
    imageBase: 'https://leen-1259789579.cos.ap-shanghai.myqcloud.com/',
    imageParameter: '?imageMogr2/thumbnail/340x/interlace/0'
  },
  onLoad() {
    this.getData()
    console.log(wx.boundingClientRect)
  },
  onPullDownRefresh() {
    wx.stopPullDownRefresh()
    this.onRefresh()
  },
  onRefresh() {
    this.reset()
    this.getData()
  },
  onReachBottom() {
    this.getData()
  },
  onImageLoad(e) {
    console.log('onImageLoad:', e)
    let { index } = e.currentTarget.dataset
    this.next(index, e.detail, true)
  },
  onImageError(e) {
    console.log('onImageError:', e)
    let { index } = e.currentTarget.dataset
    this.next(index, e.detail, false)
  },
  next(index, info, successed) {
    let { requestLength, infos, count } = this.data
    infos[index] = info
    this.setData({
      count: count + 1,
      infos
    })
    if (this.data.count === requestLength) {
      this.setData({
        count: 0,
        requestLength: 0
      })
      this.add()
    }
  },
  getData() {
    let { page, loadedAll, rows } = this.data
    return new Promise((resolve, reject) => {
      if (!loadedAll) {
        wx.showLoading({
          title: '加载中',
        })
        wx.request({
          url: `https://m.leenchina.com/api/mini/material?type=4&page=${ page }&rows=${ rows }&label=all&catalogid=all`,
          header: {
            appid: 'wx81cdfa479b01991e',
            Authorization: 'oV0VbuPl_uAD1cFnKvZeqQgV_9_g'
          },
          success: (res) => {
            console.log('素材列表：', res.data)
            let { successed, data } = res.data
            if (successed) {
              console.time()
              this.setData({
                listData2: data,
                page: page + 1,
                requestLength: data.length
              })
              if (data.length === 0) {
                wx.hideLoading()
                this.setData({
                  loadedAll: true
                })
              }
              resolve(data)
            } else {
              reject()
            }
          }
        })
      } else {
        reject()
      }
    })
  },
  add() {
    let { listData, listData2, margin, infos } = this.data
    for (let i = 0; i < infos.length; i++) {
      let { leftHeight, rightHeight } = this.data
      let height = infos[i].height || 340
      listData.push({
        id: listData2[i].id,
        height,
        top: this.getTop(),
        left: this.getLeft(),
        url: listData2[i].url
      })
      if (this.data.leftHeight <= this.data.rightHeight) {
        this.setData({
          leftHeight: leftHeight + height + margin
        })
      } else {
        this.setData({
          rightHeight: rightHeight + height + margin
        })
      }
    }
    this.setData({
      listData,
      infos: []
    })
    wx.hideLoading()
    // console.log(listData)
  },
  getLeft() {
    let { leftHeight, rightHeight, leftPosition, rightPosition } = this.data
    return leftHeight <= rightHeight ? leftPosition : rightPosition
  },
  getTop() {
    let { leftHeight, rightHeight } = this.data
    return leftHeight <= rightHeight ? leftHeight : rightHeight
  },
  reset() {
    this.setData({
      page: 1,
      infos: [],
      listData: [],
      listData2: [],
      leftHeight: 0,
      rightHeight: 0,
      loadedAll: false,
      requestLength: 0,
    })
  },
  previewImage(e) {
    let { index } = e.currentTarget.dataset
    let { imageBase, imageParameter, listData } = this.data
    let urls = []
    for (let i = 0; i < listData.length; i++) {
      urls[i] = imageBase + listData[i].url + imageParameter
    }
    wx.previewImage({
      current: urls[index],
      urls
    })
  }
})